import turtle
import time

# 设置画布
screen = turtle.Screen()
screen.title("喂食小狗")
screen.setup(width=800, height=600)
screen.bgcolor("#f0f0f0")

# 创建绘图对象
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()

# 食物对象
food = turtle.Turtle()
food.speed(0)
food.hideturtle()
food.penup()
food.goto(200, 0)

# 小狗状态
is_eating = False

def draw_dog():
    """绘制小狗"""
    pen.clear()
    
    # 身体
    pen.penup()
    pen.goto(-100, -50)
    pen.pendown()
    pen.color("#8b4513")
    pen.begin_fill()
    pen.circle(40)
    pen.end_fill()
    
    # 头
    pen.penup()
    pen.goto(-150, 10)
    pen.pendown()
    pen.begin_fill()
    pen.circle(30)
    pen.end_fill()
    
    # 耳朵
    pen.penup()
    pen.goto(-170, 40)
    pen.pendown()
    pen.begin_fill()
    pen.setheading(45)
    pen.forward(20)
    pen.setheading(180)
    pen.forward(20)
    pen.end_fill()
    
    pen.penup()
    pen.goto(-130, 40)
    pen.pendown()
    pen.begin_fill()
    pen.setheading(135)
    pen.forward(20)
    pen.setheading(0)
    pen.forward(20)
    pen.end_fill()
    
    # 眼睛
    pen.penup()
    pen.goto(-160, 20)
    pen.pendown()
    pen.color("white")
    pen.begin_fill()
    pen.circle(5)
    pen.end_fill()
    
    pen.penup()
    pen.goto(-140, 20)
    pen.pendown()
    pen.begin_fill()
    pen.circle(5)
    pen.end_fill()
    
    pen.penup()
    pen.goto(-158, 20)
    pen.pendown()
    pen.color("black")
    pen.begin_fill()
    pen.circle(2)
    pen.end_fill()
    
    pen.penup()
    pen.goto(-138, 20)
    pen.pendown()
    pen.begin_fill()
    pen.circle(2)
    pen.end_fill()
    
    # 鼻子
    pen.penup()
    pen.goto(-150, 5)
    pen.pendown()
    pen.color("black")
    pen.begin_fill()
    pen.circle(3)
    pen.end_fill()
    
    # 嘴巴
    if is_eating:
        pen.penup()
        pen.goto(-150, 0)
        pen.pendown()
        pen.setheading(-90)
        pen.forward(5)
        pen.setheading(-45)
        pen.forward(8)
        pen.penup()
        pen.goto(-150, -5)
        pen.pendown()
        pen.setheading(-135)
        pen.forward(8)
    else:
        pen.penup()
        pen.goto(-155, 0)
        pen.pendown()
        pen.setheading(-30)
        pen.forward(10)

    # 腿
    legs = [(-130, -90), (-110, -95), (-80, -95), (-60, -90)]
    for x, y in legs:
        pen.penup()
        pen.goto(x, y)
        pen.pendown()
        pen.color("#8b4513")
        pen.begin_fill()
        pen.setheading(-90)
        pen.forward(20)
        pen.setheading(0)
        pen.forward(10)
        pen.setheading(90)
        pen.forward(20)
        pen.end_fill()

def draw_food():
    """绘制食物"""
    food.clear()
    food.penup()
    food.color("#f0e68c")
    food.begin_fill()
    food.circle(10)
    food.end_fill()

def move_food_to_dog():
    """移动食物到小狗处"""
    global is_eating
    x, y = food.xcor(), food.ycor()
    
    # 食物移动到小狗嘴边
    for _ in range(50):
        x -= 4
        if x < -140:
            y = 0
        food.goto(x, y)
        draw_food()
        screen.update()
        time.sleep(0.02)
    
    # 小狗吃东西动画
    is_eating = True
    for _ in range(5):
        draw_dog()
        screen.update()
        time.sleep(0.2)
        food.hideturtle()
        time.sleep(0.2)
        draw_food()
        screen.update()
    
    # 食物消失
    food.clear()
    is_eating = False
    draw_dog()
    screen.update()

def main():
    """主函数"""
    # 初始绘制
    draw_dog()
    draw_food()
    screen.update()
    
    # 等待用户点击开始喂食
    screen.textinput("提示", "点击确定开始喂食小狗")
    
    # 执行喂食动画
    move_food_to_dog()
    
    # 显示完成信息
    pen.penup()
    pen.goto(-100, 150)
    pen.pendown()
    pen.color("black")
    pen.write("小狗吃饱啦！", font=("Arial", 16, "normal"))
    
    # 保持窗口
    turtle.done()

if __name__ == "__main__":
    main()
    